package br.com.mynage.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;

import br.com.mynage.dao.ITarefaDAO;
import br.com.mynage.dominio.Sprint;
import br.com.mynage.dominio.Tarefa;
import br.com.mynage.exception.DaoException;

public class TarefaDaoImpl extends GenericDaoImpl<Tarefa> implements ITarefaDAO {
	
	@SuppressWarnings("unchecked")
	public List<Tarefa> findTarefaBySprint(Sprint sprint) throws DaoException {
		EntityManager manager = getEntityManager();
		List<Tarefa> resul = new ArrayList<Tarefa>();
		
		try {  
			
			Session session = (Session) manager.getDelegate();
			
			String sql = "SELECT id,id_sprint,nome,descricao,data_inicial,data_final FROM tarefa " +
					"WHERE id_sprint = " + sprint.getId();
			
			List<Object[]> rs = session.createSQLQuery(sql).list();
			
			for(Object o[]:rs) {
				int i = 0;
				Tarefa t = new Tarefa();
				t.setId((Integer)o[i++]);
				t.setSprint(new Sprint());
				t.getSprint().setId((Integer)o[i++]);
				t.setNome((String)o[i++]);
				t.setDescricao((String)o[i++]);
				t.setDataInicial((Date)o[i++]);
				t.setDataFinal((Date)o[i++]);
				resul.add(t);
			}

			return resul; 
		} catch (Exception e) {
			e.printStackTrace();
			throw new DaoException();
		} finally {
			if (manager != null) 
				manager.close(); 
		}
	}

	@Override
	public Class<Tarefa> getClassType() {
		return Tarefa.class;
	}


}
